#include "stdafx.h"
#include "XCustomDrawMgr.h"

#include <Util.h>
#include <GlobalData.h>
#include <IXCustomDraw.h>
#include <IXDefaultMenuDraw.h>

XCustomDrawMgr::XCustomDrawMgr()
{
    ;
}

XCustomDrawMgr::~XCustomDrawMgr()
{
    ;
}

LRESULT XCustomDrawMgr::FilterMessage(XWND xWnd, UINT msg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
{
    switch(msg)
    {
    case WM_MEASUREITEM:
        {
            LPMEASUREITEMSTRUCT pMeasureItem = (LPMEASUREITEMSTRUCT)lParam;

            switch(pMeasureItem->CtlType)
            {
            case ODT_MENU:
                OnMenuMeasureItem(pMeasureItem);
                break;
            }

            break;
        }
    case WM_DRAWITEM:
        {
            LPDRAWITEMSTRUCT pDrawItemInfo = (LPDRAWITEMSTRUCT)lParam;

            switch(pDrawItemInfo->CtlType)
            {
            case ODT_MENU:
                OnMenuDrawItem(pDrawItemInfo);
                break;
            }

            break;
        }
    }
    return 0;
}

void XCustomDrawMgr::OnMenuMeasureItem(LPMEASUREITEMSTRUCT pInfo)
{
    CGlobalSkinData* pSkinData = CGlobalData::GetGlobalSkinData();
    if(pSkinData != NULL)
    {
        IXMenuPre* pPopup = pSkinData->GetTrackPopupMenu();
        if(pPopup != NULL)
        {
            stCustomMeasureInfo info;
            info.pControl   = pPopup;
            info.uCtlID     = pInfo->CtlID;
            info.uItemID    = pInfo->itemID;
            info.uItemWidth = pInfo->itemWidth;
            info.uItemHeight = pInfo->itemHeight;
            info.uItemData  = pInfo->itemHeight;
            XPtr<IXDefaultMenuDraw> pDraw = NULL;
            if(SUCCEEDED(Util::CreateObject(&pDraw)))
            {
                pDraw->OnMeasureItem(&info);
                pInfo->itemWidth = info.uItemWidth;
                pInfo->itemHeight = info.uItemHeight;
            }
        }
    }
}

void XCustomDrawMgr::OnMenuDrawItem(LPDRAWITEMSTRUCT pInfo)
{
    CGlobalSkinData* pSkinData = CGlobalData::GetGlobalSkinData();
    if(pSkinData != NULL)
    {
        IXMenuPre* pPopup = pSkinData->GetTrackPopupMenu();
        if(pPopup != NULL)
        {
            stCustomDrawInfo info;
            info.pControl   = pPopup;
            info.uCtlID     = pInfo->CtlID;
            info.uItemID    = pInfo->itemID;
            info.uItemState = pInfo->itemState;
            info.xCanvas    = (XCANVAS)pInfo->hDC;
            info.xWnd       = (XWND)pInfo->hwndItem;
            info.rcItem     = pInfo->rcItem;
            info.uItemData  = pInfo->itemData;
            XPtr<IXDefaultMenuDraw> pDraw = NULL;
            if(SUCCEEDED(Util::CreateObject(&pDraw)))
            {
                pDraw->OnDrawItem(&info);
            }
        }
    }
}
